package com.bltec.emergency.controller.recordController;

import com.alibaba.fastjson.JSON;
import com.bltec.emergency.common.Constant;
import com.bltec.emergency.common.ResponseJson;
import com.bltec.emergency.model.RecordTrain;
import com.bltec.emergency.service.recordService.RecordTrainService;
import com.bltec.emergency.service.mgrService.UserService;
import com.bltec.emergency.utils.DateUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/train")
@RequiresPermissions("project:record")
public class TrainController {
    @Resource
    private RecordTrainService recordTrainService;
    @Resource
    private UserService userService;
    /**
     * 获得所有train
     */
    @RequestMapping("getAllTrain")
    public ResponseJson getTrain() {
        try {
            List<RecordTrain> recordTrain = recordTrainService.getAll();
            return new ResponseJson(Constant.OK, JSON.toJSON(recordTrain));
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJson(Constant.FAIL, e.getMessage());
        }
    }

    /**
     * 添加train
     * #{trainName},#{trainUnit},#{trainDepartment},#{trainTime},#{trainPosition},#{participantsNum},#{createBy},#{createTime},#{enable}
     */
    @RequestMapping("addTrain")
    @RequiresPermissions("record:insert")
    public ResponseJson addTrain(String trainName, String trainUnit, String trainDepartment, Date trainTime, String trainPosition, Integer participantsNum) {
        try {
            RecordTrain train = new RecordTrain();
            train.setTrainName(trainName);
            train.setTrainUnit(trainUnit);
            train.setTrainDepartment(trainDepartment);
            train.setTrainTime(trainTime);
            train.setTrainPosition(trainPosition);
            train.setParticipantsNum(participantsNum);
            train.setEnable(true);
            train.setCreateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            train.setCreateTime(DateUtil.dateNow());
            recordTrainService.add(train);
            return new ResponseJson(Constant.OK, "创建用户成功");
        } catch (Exception e) {
            return new ResponseJson(Constant.FAIL, "创建用户失败" + e.getMessage());
        }
    }

    /**
     * 修改train
     */
    @RequiresPermissions("record:update")
    @RequestMapping("updateTrain")
    public ResponseJson updateTrain(Long id, String trainName, String trainUnit, String trainDepartment, Date trainTime, String trainPosition, Integer participantsNum) {
        try {
            RecordTrain train = recordTrainService.getTrainById(id);
            train.setTrainName(trainName);
            train.setTrainUnit(trainUnit);
            train.setTrainDepartment(trainDepartment);
            train.setTrainTime(trainTime);
            train.setTrainPosition(trainPosition);
            train.setParticipantsNum(participantsNum);
            train.setUpdateBy(userService.getIdByUserName((String) SecurityUtils.getSubject().getPrincipal()));
            train.setUpdateTime(DateUtil.dateNow());
            recordTrainService.updateTrainById(train);
            return new ResponseJson(Constant.OK, JSON.toJSON(train));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.UPDATE_ERROR;
        }
    }

    /**
     * 删除train
     */
    @RequiresPermissions("record:delete")
    @RequestMapping("deleteTrain")
    public ResponseJson setDisable(Long id) {
        if (null != recordTrainService.getTrainById(id)) {
            if (recordTrainService.deleteTrainById(id)) {
                return new ResponseJson(Constant.OK, "删除成功");
            } else {
                return new ResponseJson(Constant.FAIL, "删除失败");
            }
        } else {
            return new ResponseJson(Constant.OK, "删除成功");
        }
    }

}
